﻿
Partial Class Login
    Inherits System.Web.UI.UserControl

    Protected Sub Login1_Authenticate(sender As Object, e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate
        Dim authuser As New User(Login1.UserName)
        If authuser.Authenticate(Login1.Password) Then
            e.Authenticated = True
            'Store user in session
            Session("UserObject") = authuser
            Session("BadAttempts") = 0
        Else
            e.Authenticated = False
        End If

    End Sub

    Protected Sub Login1_LoginError(sender As Object, e As System.EventArgs) Handles Login1.LoginError
        If Login1.UserName <> String.Empty Then
            Dim authuser As New User(Login1.UserName)
            If authuser.isLocked Then
                Login1.FailureText = "Your account is locked." & _
                    "<br/><a href='ForgotPass.aspx' title='Forgot Password?'>Forgot Password?</a>"
            Else
                If IsNothing(Session("BadAttempts")) Then
                    Session("BadAttempts") = 1
                Else
                    Session("BadAttempts") += 1
                    If Session("BadAttempts") = 3 Then
                        Login1.FailureText = "For security the account will be locked out on next invalid login." & _
                            "<br/><a href='ForgotPass.aspx' title='Forgot Password?'>Forgot Password?</a>"
                    ElseIf Session("BadAttempts") >= 4 Then
                        'Lock the user out
                        authuser.LockAccount(True)
                        Login1.FailureText = "Your account is locked." & _
                            "<br/><a href='ForgotPass.aspx' title='Forgot Password?'>Forgot Password?</a>"
                    End If
                End If
            End If
        End If
    End Sub
End Class
